<?php defined('BASEPATH') OR exit('No direct script access allowed');


class Menu_model extends Base_model
{
	protected $_table_name = 'menu';
    protected $_key_name = 'id';

    protected $_menu_lists_arr        = NULL;
	
	public function __construct()
    {
        parent::__construct();
		
    }

    public function get_role_menu()
    {
        $this->select("id,name,parent_id as parentId,url,perms,type,icon,order_num as orderNum ");
        $this->where("dtime",0);
        return $this->all();
    }

    public function saves($data_arr = NULL)
    {
        if($data_arr == NULL) return false;

        $data_arr["ctime"] = time();
        return $this->replace($data_arr);
    }

    public function dels($ids_arr = NULL)
    {
        if($ids_arr == NULL) return false;
        $this->where_in($this->_key_name,$ids_arr);
        $data_arr['dtime'] = time();
        return $this->update($data_arr);
    }

    public function get_alls_menu($type = NULL,$isroot = false,$menu_ids = NULL)
    {
        $this->where_in("type",$type);

        if($isroot == false)
        {
            $this->where_in("id",$menu_ids);
        }

        $this->where("dtime",0);

        return $this->all();
    }

    public function get_alls_menu_tree($type = NULL,$isroot = false,$menu_ids = NULL)
    {
        return $this->_digui_menu(0,$type,$isroot,$menu_ids,$parentName = NULL);
    }

    private function _digui_menu($pid = 0,$type = NULL,$isroot = false,$menu_ids = NULL,$level = 0,$parentName = NULL)
    {
        $this->select("id,name,parent_id as parentId,url,perms,type,icon,order_num as orderNum ");

        $this->where_in("type",$type);
        if($isroot == false)
        {
            $this->where_in("id",$menu_ids);
        }

        $this->where("dtime",0);
        $this->where("parent_id",$pid);
        $this->order_by("order_num","DESC");

        $data_arr =  $this->all();

        if($data_arr)
        {
            foreach($data_arr as &$val)
            {
                $val->level = $level;
                $val->parentName = $parentName;
                $val->children = $this->_digui_menu($val->id,$type,$isroot,$menu_ids,$level+1,$val->name);
            }
        }
        return $data_arr;

    }

}
/* End of file base_model.php */
/* Location: ./system/application/models/base_model.php */
